// pages/fillin_info/fillin_info.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // price: ['请选择充值金额', '0元','10元', '20元', '50元', '100元'],
    price:'100',
    priceindex: 0,
    // deposit: ['0元', '10元', '20元', '50元', '100元'],
    // depositindex: 0,
    v_id:'',
    village_name: '清选择园区地址',
    my:true,
    province:'',
    city:'',
    area:'',
    card_number:'',
    full_name:'',
    tel:'',
    IDcard:'',
    address:' ',
    recharge_money:'0元',
    deposit_s: 0,
    _num:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.v_id != undefined){
      that.setData({
        v_id: options.v_id,
        village_name: options.village_name,
        // address: options.address,
        province: options.province,
        city: options.city,
        area: options.area
      });
    }

    app.ajax({
      url: 'Partner/WaterCard/getDefaultVillage',
      data: {},
      load: true,
      success: function (res) {
        if (res.data.code == 1000) {
          console.log(res.data)
          that.setData({
            village_name: res.data.data.village_name,
            province: res.data.data.province,
            city: res.data.data.city,
            area: res.data.data.area,
          })
        }
      }
    });
    that.cardDeposit(); 

    // console.log(that.data);
    
  },
  entityCardApplication(){
    var that = this;
    // if (that.data.province == '' || that.data.city == '' || that.data.area == ''){
    //   app.showToast('请选择地址', "none", 2000, function () {});
    //   return false;
    // }
    if(that.data.village_name==""){
      app.showToast('请选择园区', "none", 2000, function () { });
      return false;
    }
    if (that.data.card_number == '') {
      app.showToast('请输入或扫码水卡卡号', "none", 2000, function () { });
      return false;
    }
    if (that.data.full_name == '') {
      app.showToast('请输入姓名', "none", 2000, function () { });
      return false;
    }
    if (that.data.tel == '') {
      app.showToast('请输入电话', "none", 2000, function () { });
      return false;
    }
    // if (that.data.IDcard == '') {
    //   app.showToast('请输入证件号码', "none", 2000, function () { });
    //   return false;
    // }
    // if (that.data.address == '') {
    //   app.showToast('请输入详细地址', "none", 2000, function () { });
    //   return false;
    // }
    if (that.data.price == '') {
      app.showToast('请选择充值金额', "none", 2000, function () { });
      return false;
    }
    if (that.data.deposit_s == '' || that.data.deposit_s == null ){
      app.showToast('押金不能为空');
      return false;  
    } else if (that.data.deposit_s < 0){
      app.showToast('押金不能为负数');
      return false;
    }
    app.ajax({
      url: 'Partner/WaterCard/entityCardApplication',
      data: {
        province: that.data.province,
        city: that.data.city,
        area: that.data.area,
        village_name: that.data.village_name,
        full_name: that.data.full_name,
        tel: that.data.tel,
        IDcard: that.data.IDcard,
        card_number: that.data.card_number,
        recharge_money: that.data.price,
        deposit: that.data.deposit_s,
        address: that.data.address
      },
      load: true,
      success: function (res) {
        if (res.data.code == 1000) {         
          app.showToast(res.data.message, "none", 2000, function () {
            wx.navigateTo({
              url: '../cardinfo/cardinfo?info=' + JSON.stringify(that.data),
            })
           });
          return false;
        }else{
          app.showToast(res.data.message, "none", 2000, function () { });
        }
      }
    }) 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //水卡号
  card_number: function (e) {
    this.setData({
      card_number: e.detail.value
    })
  },
  //姓名
  full_name: function (e) {
    this.setData({
      full_name: e.detail.value
    })
  },
  //电话
  tel: function (e) {
    this.setData({
      tel: e.detail.value
    })
  }, 
  //证件号码
  IDcard: function (e) {
    this.setData({
      IDcard: e.detail.value
    })
  },
  //详细地址
  address: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  //押金
  depositfun(e){
    this.setData({
      deposit_s: e.detail.value
    })
  },
  // 其他金额
  price:function(e){
    this.setData({
      _num: 0,
      price: e.detail.value
    })
  },
  //获取水卡押金
  cardDeposit:function(){
    var that=this;
    app.ajax({
      url: 'Common/Common/cardDeposit',
      data: {},
      load: true,
      success: function (res) {
        if (res.data.code == 1000) {          
          that.setData({
            deposit_s: res.data.data
          })
        }
      }
    }) 
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 选择金额
  // choiceprice: function (e) {
  //   //改变index值，通过setData()方法重绘界面
  //   this.setData({
  //     priceindex: e.detail.value,
  //     recharge_money: this.data.price[e.detail.value]
  //   });
  // },

  // 选择押金
  // choicedeposit: function (e) {
  //   //改变index值，通过setData()方法重绘界面
  //   this.setData({
  //     depositindex: e.detail.value,
  //     deposit_s: this.data.deposit[e.detail.value]
  //   });
  // },

  scan: function () {
    wx.scanCode({
      success: (res) => {
        console.log("扫码结果");
        console.log(res);
        this.setData({
          img: res.result
        })
      },
      fail: (res) => {
        console.log(res);
      }
    })  
  },

  // 添加地址
  addaddress:function(){
    wx.navigateTo({
      url: '../addresslist/addresslist',
    })
  },

  menuClick: function (e) {
    var num = e.target.dataset.num;
    this.setData({
      _num: e.target.dataset.num
    })
    if (num==1){
      this.setData({
        price: '100'
      })
    } else if (num == 2){
      this.setData({
        price: '200'
      })
    } else if (num == 3){
      this.setData({
        price: '300'
      })
    }
  },
})